package com.crm.customer.service;

import com.crm.common.mapper.LinkmanMapper;
import com.crm.common.pojo.Linkman;
import com.crm.global.exception.CrmException;
import com.crm.global.exception.ExceptionEnum;
import com.crm.id.increment.IdWorker;
import com.crm.mybatis.separation.Read;
import com.crm.mybatis.separation.Write;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class LinkmanService  {
    @Autowired
    private LinkmanMapper linkmanMapper;


    @Read
    public List<Linkman> findAll(Long customerId) {
        Example example = new Example(Linkman.class);
        example.createCriteria().andEqualTo("customerId",customerId);
        List<Linkman> linkmen = linkmanMapper.selectByExample(example);
        return linkmen;
    }

   @Write
    public void saveOrUpdate(Linkman linkman) {
        if (linkman == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        if (linkman.getId() == null) {
            linkman.setId(new IdWorker().nextId());
            linkmanMapper.insert(linkman);
        } else {
            linkmanMapper.updateByPrimaryKey(linkman);
        }
    }

    @Write
    public void deleteById(Long id) {
        if (id == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        linkmanMapper.deleteByPrimaryKey(id);
    }

    @Write
    public void deleteByCustomerId(Long customerId) {
        if (customerId == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        Example example = new Example(Linkman.class);
        example.createCriteria().andEqualTo("customerId",customerId);
        linkmanMapper.deleteByExample(example);
    }

}
